import datetime

from common.yamlLoad import YamlRead
from business_common.requestFunc import BusinessRequest

env_config = YamlRead().env_config()
api_config = YamlRead().api_config()

def all_note_clear(user_id, sid):
    # 先请求home页面的notes，获取到所有notes
    host = env_config['host']
    url = host + f'/v3/notesvr/user/{user_id}/home/startindex/0/rows/99999/notes'
    res = BusinessRequest.get(url, sid)
    for note in res.json()['webNotes']:
        note_id = note['noteId']
        # 请求delete 接口，删除所有notes
        del_url = host + api_config['delete_note']['path']
        body = {'noteId':note_id}
        BusinessRequest.post(del_url, user_id=user_id, sid = sid, body=body)

def calender_notes_delete(user_id, sid):
    # 先获取当前日历下的所有便签
    host = env_config['host']
    get_notes_url = host + f'/notesvr/web/getnotes/remind'
    # 获取当前日期
    now = datetime.datetime.now()
    # 格式化当前年月为 2025/02 格式
    month = now.strftime("%Y/%m")
    # 获取当前月的第一天
    first_day = datetime.datetime(now.year, now.month, 1)
    # 获取下个月的第一天
    next_month = first_day.replace(day=28) + datetime.timedelta(days=4)
    last_day = next_month - datetime.timedelta(days=next_month.day)

    # 将日期转换为时间戳（毫秒）
    remind_start_time = int(first_day.timestamp() * 1000)
    remind_end_time = int(last_day.replace(hour=23, minute=59, second=59, microsecond=999999).timestamp() * 1000)

    body = {
        "rows":999,
        "startIndex":0,
        "month":month,
        "remindStartTime":remind_start_time,
        "remindEndTime":remind_end_time
    }
    res = BusinessRequest.post(get_notes_url, user_id=user_id, sid = sid, body=body)
    for note in res.json()['webNotes']:
        note_id = note['noteId']
        del_url = host + api_config['delete_note']['path']
        body = {'noteId': note_id}
        BusinessRequest.post(del_url, user_id=user_id, sid=sid, body=body)

def clean_recyclebin(user_id, sid):
    # 清空回收站
    host = env_config['host']
    clean_note_url = host + api_config['clear_recyclebinNotes']['path']
    body = {
        'noteIds': [-1]
    }
    BusinessRequest.post(clean_note_url, user_id=user_id, sid=sid, body=body)

if __name__ == '__main__':
    all_note_clear(env_config['user_id'], env_config['wps_sid'])
    calender_notes_delete(env_config['user_id'], env_config['wps_sid'])